【nacos】5.2 @RefreshScope + @Value 注解 自动刷新配置 | 您所在的位置:网站首页 › spring 热加载jar 不支持service注解 › 【nacos】5.2 @RefreshScope + @Value 注解 自动刷新配置 |
返回首页 > 【笔记】Spring Cloud Alibaba Nacos 接上节:spring cloud + Nacos 实现统一配置管理 1. 问题:如何不启动spring boot项目自动更新@Value 注解的值? 2. 方案:类/方法/属性 上使用@RefreshScope 依赖:请看参考上一节 3. 实战 nacos上更新mysqlport值,使用接口去访问,并对比nacos上修改配置前后的返回值 package com.hzd.controller; import com.alibaba.nacos.api.config.annotation.NacosValue; import com.privatecloud.core.dto.JsonResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Api(tags = "测试管理", value = "如:测试xxj-job的定时任务") @RestController @RequestMapping("/test") @RefreshScope public class TestController { @Value("${mysqlport}") private String port; @NacosValue("${mysqlport}") private String port1; /** * @return Boolean * @explain 插入或新建图片标注 * @author lijiong * @time 2022年05月26日 */ @GetMapping("/nacos/fresh") @ApiOperation(value = "测试nacos配置自动刷新", notes = "作者:lijiong") public JsonResult test() { JsonResult result = new JsonResult(); String msg = "@value mysqlport:" + port + "; @NacosValue mysqlport:" + port1; return result.success(msg); } } 4. 运行结果 修改前 3306nacos低版本 @NacosValue(value = "${token.expiration}", autoRefreshed = true) com.alibaba.boot nacos-discovery-spring-boot-starter 0.27nacos高版本,官方示例使用 @Value @Value(value = "${mqtt.broker-url}") com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.8.RELEASE pom import 6. 思考:如果修改一些需要预加载的配置呢,如下场景 修改连接Mqtt服务器,并订阅修改连接TCP服务器修改TCP客户端端口修改mysql等数据库地址,端口,账号,密码是否不用启动服务器能立即生效? 如何才能生效? |
CopyRight 2018-2019 实验室设备网 版权所有 |